package com.beemans.topon.demo.ui.view

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import com.anythink.nativead.api.ATNativeImageView
import com.anythink.nativead.api.ATNativeMaterial
import com.anythink.nativead.api.ATNativePrepareExInfo
import com.anythink.nativead.api.ATNativePrepareInfo
import com.beemans.topon.demo.R
import com.beemans.topon.ads.INativeSelfRender
import com.blankj.utilcode.util.SizeUtils

/**
 * @author tiamosu
 * @date 2022/10/31
 */
class CustomNativeRender(private val selfRenderView: View) : INativeSelfRender {

    override fun onBindView(
        context: Context,
        atNativeMaterial: ATNativeMaterial,
        prepareInfo: ATNativePrepareInfo
    ) {
        val padding: Int = SizeUtils.dp2px(5f)
        selfRenderView.setPadding(padding, padding, padding, padding)
        selfRenderView.isVisible = true

        val titleView = selfRenderView.findViewById<TextView>(R.id.native_ad_title)
        val descView = selfRenderView.findViewById<TextView>(R.id.native_ad_desc)
        val ctaView = selfRenderView.findViewById<TextView>(R.id.native_ad_install_btn)
        val adFromView = selfRenderView.findViewById<TextView>(R.id.native_ad_from)
        val iconArea = selfRenderView.findViewById<FrameLayout>(R.id.native_ad_image)
        val contentArea =
            selfRenderView.findViewById<FrameLayout>(R.id.native_ad_content_image_area)
        val logoView = selfRenderView.findViewById<ATNativeImageView>(R.id.native_ad_logo)
        val closeView = selfRenderView.findViewById<ImageView>(R.id.native_ad_close)

        val clickViewList: MutableList<View> = ArrayList() //click views

        val title = atNativeMaterial.title
        // title
        if (!TextUtils.isEmpty(title)) {
            titleView.text = title
            prepareInfo.titleView = titleView //bind title
            clickViewList.add(titleView)
            titleView.visibility = View.VISIBLE
        } else {
            titleView.visibility = View.GONE
        }

        val descriptionText = atNativeMaterial.descriptionText
        if (!TextUtils.isEmpty(descriptionText)) {
            // desc
            descView.text = descriptionText
            prepareInfo.descView = descView //bind desc
            clickViewList.add(descView)
            descView.visibility = View.VISIBLE
        } else {
            descView.visibility = View.GONE
        }

        // icon
        val adIconView = atNativeMaterial.adIconView
        val iconImageUrl = atNativeMaterial.iconImageUrl
        iconArea.removeAllViews()
        val iconView = ATNativeImageView(context)
        if (adIconView != null) {
            iconArea.addView(adIconView)
            prepareInfo.iconView = adIconView //bind icon
            clickViewList.add(adIconView)
            iconArea.visibility = View.VISIBLE
        } else if (!TextUtils.isEmpty(iconImageUrl)) {
            iconArea.addView(iconView)
            iconView.setImage(iconImageUrl)
            prepareInfo.iconView = iconView //bind icon
            clickViewList.add(iconView)
            iconArea.visibility = View.VISIBLE
        } else {
            iconArea.visibility = View.INVISIBLE
        }

        // cta button
        val callToActionText = atNativeMaterial.callToActionText
        if (!TextUtils.isEmpty(callToActionText)) {
            ctaView.text = callToActionText
            prepareInfo.ctaView = ctaView //bind cta button
            clickViewList.add(ctaView)
            ctaView.visibility = View.VISIBLE
        } else {
            ctaView.visibility = View.GONE
        }

        // media view
        val mediaView = atNativeMaterial.getAdMediaView(contentArea)
        val mainImageHeight = atNativeMaterial.mainImageHeight
        val mainImageWidth = atNativeMaterial.mainImageWidth

        val realMainImageWidth = context.resources.displayMetrics.widthPixels - SizeUtils.dp2px(10f)
        val mainImageParam = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT
        )
        val realMainHeight: Int
        if (mainImageWidth > 0 && mainImageHeight > 0 && mainImageWidth > mainImageHeight) {
            realMainHeight = realMainImageWidth * mainImageHeight / mainImageWidth
            mainImageParam.width = realMainImageWidth
            mainImageParam.height = realMainHeight
        } else {
            mainImageParam.width = FrameLayout.LayoutParams.MATCH_PARENT
            mainImageParam.height = realMainImageWidth * 600 / 1024
        }

        contentArea.removeAllViews()
        if (mediaView != null) {
            if (mediaView.parent != null) {
                (mediaView.parent as ViewGroup).removeView(mediaView)
            }
            mainImageParam.gravity = Gravity.CENTER
            mediaView.layoutParams = mainImageParam
            contentArea.addView(mediaView, mainImageParam)
            clickViewList.add(mediaView)
            contentArea.visibility = View.VISIBLE
        } else if (!TextUtils.isEmpty(atNativeMaterial.mainImageUrl)) {
            val imageView = ATNativeImageView(context)
            imageView.setImage(atNativeMaterial.mainImageUrl)
            imageView.layoutParams = mainImageParam
            contentArea.addView(imageView, mainImageParam)
            prepareInfo.mainImageView = imageView //bind main image
            clickViewList.add(imageView)
            contentArea.visibility = View.VISIBLE
        } else {
            contentArea.removeAllViews()
            contentArea.visibility = View.GONE
        }

        //Ad Logo
        val adChoiceIconUrl = atNativeMaterial.adChoiceIconUrl
        val adLogoBitmap = atNativeMaterial.adLogo
        if (!TextUtils.isEmpty(adChoiceIconUrl)) {
            logoView.setImage(adChoiceIconUrl)
            prepareInfo.adLogoView = logoView //bind ad choice
            logoView.visibility = View.VISIBLE
        } else if (adLogoBitmap != null) {
            logoView.setImageBitmap(adLogoBitmap)
            logoView.visibility = View.VISIBLE
        } else {
            logoView.setImageBitmap(null)
            logoView.visibility = View.GONE
        }

        val adFrom = atNativeMaterial.adFrom
        // ad from
        if (!TextUtils.isEmpty(adFrom)) {
            adFromView.text = adFrom
            adFromView.visibility = View.VISIBLE
        } else {
            adFromView.visibility = View.GONE
        }
        prepareInfo.adFromView = adFromView //bind ad from

        val layoutParams = FrameLayout.LayoutParams(
            SizeUtils.dp2px(40f),
            SizeUtils.dp2px(10f)
        ) //ad choice

        layoutParams.gravity = Gravity.BOTTOM or Gravity.END
        prepareInfo.choiceViewLayoutParams = layoutParams //bind layout params for ad choice
        prepareInfo.closeView = closeView //bind close button
        prepareInfo.clickViewList = clickViewList //bind click view list

        if (prepareInfo is ATNativePrepareExInfo) {
            val creativeClickViewList: MutableList<View> = ArrayList() //click views
            creativeClickViewList.add(ctaView)
            prepareInfo.creativeClickViewList = creativeClickViewList //bind custom view list
        }
    }

    override fun getSelfRenderView(): View {
        return selfRenderView
    }
}